﻿//Copyright (C) <2013>  <jonathan cleeve norton> All Rights Reserved 
//Contact jon.norton@fin-plus.co.uk website <http://www.fin-plus.co.uk/>
using System;
using System.Collections.Generic;
using QLNet;
using FinPlusAssembler;
using p = FinPlusCompQuant.QLConvParser;

namespace FinPlusCompQuant
{
    public class BermudanSwaptionTrade : Trade
    {
        public Trade VanillaSwap { get; private set; }

        //construct
        public BermudanSwaptionTrade(string marketName, string cacheName, string id, string curveName, string volSurfName, double nominal, DateTime start, 
            DateTime maturity, double strike, string payRec, string index, double spread, string  fixLegFrq, string fltLegFrq, string fixLegConv,  
            string fltLegConv, string fixLegDayCount, string fltLegDayCount, string holidays, bool endOfMonth = false)
        {
            Id = id;
            var market = Markets.Instance.GetMarket(marketName);
            var cache = Caches.Instance.GetCache(cacheName);
            var indx = market.GetIndex(index);
            var type = p.PayRec(payRec).EnumParse<VanillaSwap.Type>();
            var calendar = p.Calendar(holidays);
		
		    var fixedSchedule = new Schedule(start, maturity, new Period(p.Freq(fixLegFrq)), calendar, p.BizConv(fixLegConv), p.BizConv(fixLegConv), DateGeneration.Rule.Forward, endOfMonth);
            var floatSchedule = new Schedule(start, maturity, new Period(p.Freq(fltLegFrq)), calendar, p.BizConv(fltLegConv), p.BizConv(fltLegConv), DateGeneration.Rule.Forward, endOfMonth);
            var swap = new VanillaSwap(type, nominal, fixedSchedule, strike, p.DayCount(fixLegDayCount), floatSchedule, indx, spread, p.DayCount((fltLegDayCount)));
		
            var bermudanDates = new List<Date>();
            var leg = swap.fixedLeg();
            for (int i = 0; i < leg.Count; i++)
            {
                var coupon = (Coupon)leg[i];
                bermudanDates.Add(coupon.accrualStartDate());
            }

            var bermudanExercise = new BermudanExercise(bermudanDates);
            Underlying = new Swaption(swap, bermudanExercise);

            cache.Add(id, this, volSurfName);
        }
    }
}
